package com.yswb.server;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

public class TcpServer {
	//private static final String IP = "127.0.0.1";
	private static final int PORT = 7878;
	/**用于分配处理业务线程的线程组个数 */
	protected static final int BIZGROUPSIZE = Runtime.getRuntime().availableProcessors()*2;	//默认
	/** 业务出现线程大小*/
	protected static final int BIZTHREADSIZE = 1;
	
	private static EventLoopGroup bossGroup=new NioEventLoopGroup(BIZTHREADSIZE);
	private static EventLoopGroup workerGroup=new NioEventLoopGroup(BIZTHREADSIZE);
	public static void run(){
		try {
			ServerBootstrap b=new ServerBootstrap();
			b.group(bossGroup, workerGroup);
			b.channel(NioServerSocketChannel.class);
			b.childHandler(new HelloServerInitializer());
			// 服务器绑定端口监听
        	ChannelFuture f = b.bind(PORT).sync();
            // 监听服务器关闭监听
			f.channel().closeFuture().sync();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally{
			bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
		}
	}
	
	public static void main(String[] args) {
		run();
	}
}
